from traceback import print_tb
import numpy as np
import sys
import random
import time
from PIL import Image, ImageDraw
import os
import cv2


im = Image.open("feifei.bmp")
im_hide = Image.open("dct.bmp")


im_array = np.array(im_hide)
im_array = np.reshape(im_array, (-1, 3)) # 变二维数组
# print(im_array)
# print(im_array[0][0])
# for i in im_array:
#     print(i)
# print(im_array[0][0])
# print(im_array[0][1])

width, height = im.size
out = Image.new('RGB', (width, height))  # 输出图像
draw = ImageDraw.Draw(out)  # 画图

def dct():
    cnt = 0  #用于记录被隐藏图片图片像素点数，写入一个就自增
    for k in range(64):
        for l in range(64):
            dct_array = np.zeros(64).reshape(8, 8)
            for y in range(8):
                for x in range(8):
                    pix = im.getpixel((k * 8 + x, l * 8 + y))  # 依次获取原图像素点                  
                    dct_array[x][y] = pix[2]

            print("dct前88矩阵:")
            print(dct_array)

            dct_dct = cv2.dct(dct_array) # dct变换
            print("变化前:")
            print(dct_dct)
            dct_dct[3][3] = im_array[cnt][0]
            cnt += 1
            dct_dct[3][4] = im_array[cnt][0]
            cnt += 1
            dct_dct[4][3] = im_array[cnt][0]
            cnt += 1
            dct_dct[4][4] = im_array[cnt][0]
            cnt += 1
            print("变化后:")
            print(dct_dct)

            dct_idct = cv2.idct(dct_dct)
            print("反dct变换后的88矩阵")
            print(dct_idct)


            for y in range(8):
                for x in range(8):
                    pix = im.getpixel((k * 8 + x, l * 8 + y))  # 依次获取原图像素点            
                    co_list = list()
                    co_list.append(int(pix[0]))
                    co_list.append(int(pix[1]))
                    co_list.append(int(dct_idct[x][y]))
                    co_tuple = tuple(co_list)
                    draw.point((k * 8 + x, l * 8 + y), fill=co_tuple)  # 颜色元组，并且在（x,y）上画一个像素点
                    out.save("output_rgb", 'bmp') # 保存到的图片的名称

            print("进度:" + str(cnt))
            


dct()


